package com.pb.camera;

import com.lidroid.xutils.HttpUtils;
import com.pb.camera.fragment.HomeFragment;
import com.pb.camera.fragment.MessageFragment;
import com.pb.camera.fragment.MyFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class HomePageActivity extends FragmentActivity implements
        OnClickListener {
    private HomeFragment home_page_fragment;
    private MessageFragment message_fragment;
    private MyFragment my_fragment;
    private FragmentManager fg;
    private TextView text_home;
    private TextView text_mesage;
    private TextView text_my;
    private HttpUtils httpUtils;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        httpUtils = new HttpUtils();
        text_home = (TextView) findViewById(R.id.home_page_text_home);
        text_mesage = (TextView) findViewById(R.id.home_page_text_message);
        text_my = (TextView) findViewById(R.id.home_page_text_personal_center);
        text_home.setOnClickListener(this);
        text_home.setSelected(true);
        text_mesage.setOnClickListener(this);
        text_my.setOnClickListener(this);
        home_page_fragment = new HomeFragment();
        fg = getSupportFragmentManager();
        FragmentTransaction ft = fg.beginTransaction();
        ft.add(R.id.home_page_fragment_container, home_page_fragment);
        ft.commit();
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        FragmentTransaction ft = fg.beginTransaction();
        hiddenFragment(ft);
        setSelected(false);
        switch (view.getId()) {
            case R.id.home_page_text_home:
                if (home_page_fragment == null) {
                    home_page_fragment = new HomeFragment();
                    ft.add(R.id.home_page_fragment_container, home_page_fragment,
                            "home");
                } else {
                    ft.show(home_page_fragment);
                }
                view.setSelected(true);
                break;
            case R.id.home_page_text_message:
                if (message_fragment == null) {
                    message_fragment = new MessageFragment();
                    ft.add(R.id.home_page_fragment_container, message_fragment,
                            "message");
                } else {
                    ft.show(message_fragment);
                }
                view.setSelected(true);
                break;

            case R.id.home_page_text_personal_center:
                if (my_fragment == null) {
                    my_fragment = new MyFragment();
                    ft.add(R.id.home_page_fragment_container, my_fragment, "my");
                } else {
                    ft.show(my_fragment);
                }
                view.setSelected(true);
                break;
            default:
                break;
        }
        ft.commitAllowingStateLoss();
    }

    private void hiddenFragment(FragmentTransaction ft) {
        if (home_page_fragment != null) {
            ft.hide(home_page_fragment);
        }
        if (message_fragment != null) {
            ft.hide(message_fragment);
        }
        if (my_fragment != null) {
            ft.hide(my_fragment);
        }
    }

    /**
     * 选取摄像头设备
     */
    private void sortDevice() {}

    /**
     * @return 返回当前显示的是那么界面是首页, 还是设备或设置
     */
    private int getSelectedPage() {
        if (text_home.isSelected()) {
            return 0;
        } else if (text_mesage.isSelected()) {
            return 1;
        } else if (text_my.isSelected()) {
            return 2;
        }
        return 0;
    }

    private void setSelected(boolean selected) {
        text_home.setSelected(selected);
        text_mesage.setSelected(selected);
        text_my.setSelected(selected);
    }

    // private void intiFragment(AllRoom rooms) {
    // List<Room> data;
    // if (rooms==null) {
    // data=new ArrayList<Room>();
    // }else {
    // data=rooms.getData();
    // }
    // FragmentTransaction ft = fg.beginTransaction();
    // if (data.size() == 0) {
    // if (nothing_fragment == null) {
    // nothing_fragment = new NothingFragment();
    // ft.add(R.id.home_page_fragment_container, nothing_fragment);
    // } else {
    // ft.show(nothing_fragment);
    // }
    // } else {
    // if (home_page_fragment == null) {
    // home_page_fragment = new HomeFragment();
    // home_page_fragment.initRooms(data);
    // ft.add(R.id.home_page_fragment_container, home_page_fragment);
    // } else {
    // ft.show(home_page_fragment);
    // }
    // }
    // ft.commitAllowingStateLoss();
    // }

    // /**获取有的房间号
    // * @param url
    // * @param mode
    // */
    // private void getAllRoom(String url,int mode )
    // {
    // httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {
    //
    // @Override
    // public void onFailure(HttpException arg0, String arg1) {
    // // TODO Auto-generated method stub
    // Toast.makeText(HomePageActivity.this, "网络访问失败,请检查你的网络",
    // Toast.LENGTH_SHORT);
    // }
    //
    // @Override
    // public void onSuccess(ResponseInfo<String> arg0) {
    // // TODO Auto-generated method stub
    // final AllRoom allRoom = JsonUtil.parseJsonToBean(arg0.result,
    // AllRoom.class);
    // if (allRoom.getErrmsg().equals("")||(allRoom.getErrmsg()==null)) {
    // HomePageActivity.this.runOnUiThread(new Runnable() {
    // @Override
    // public void run() {
    // // TODO Auto-generated method stub
    // selfRooms=allRoom.getData();
    // intiFragment(allRoom);
    // }
    // });
    // }
    // }
    // });
    // }
}
